<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\BaseController;
use app\service\StoreBackService;
use app\service\StoreService;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Request;

class StoreBackController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
      $page = $request->param('page', 1);
      $pageSize = $request->param('pageSize', env('pager.default.size'));
      $backEnd = $request->param('backEnd', '');
      $backStart = $request->param('backStart', '');
      $backStart = $backStart ? date('Y-m-d', strtotime($backStart)) : '';
      $backEnd = $backEnd ? date('Y-m-d', strtotime($backEnd) + (24 * 60 * 60)) : '';

      $conditions = [];
      if ($backStart) {
        $conditions[] = ['back_time', $backStart, '>='];
      }
      if ($backEnd) {
        $conditions[] = ['back_time', $backEnd, '<'];
      }
      $order = ['back_time' => 'desc', 'id' => 'desc'];
      $list = StoreBackService::getInstance()->getDataByCondition($conditions, $order);
      // 分页
      $count = count($list); //总条数
      $start=intval(($page - 1) * $pageSize); //偏移量，当前页-1乘以每页显示条数
      $pager = [
        'totalCount' => $count,
        'pageSize' => intval($pageSize),
        'currPage' => intval($page),
        'totalPage' => intval($count/$pageSize)+1
      ];
      $data = array_slice($list, $start, intval($pageSize));
      return $this->send200ResponseWithPager($data,$pager);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $detail = $request->param('data', []);
      $back_date = $request->param('back_date', '');
      if (!$detail) {
        return $this->send400Response('缺少data参数');
      }
      if (!$back_date) {
        return $this->send400Response('缺少back_date参数');
      }
      $back_date = date('Y-m-d', strtotime($back_date));
      $data = [
        'detail' => $detail,
        'back_time' => $back_date
      ];
      $result = StoreBackService::getInstance()->createMultiBack($data);
      if (!$result['status']) {
        return $this->send400Response($result['msg']);
      }
      return $this->send200Response('ok');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      // 获取退货单
      $data = StoreBackService::getInstance()->getDataByCondition([['id', $id, '=']]);
      $data = isset($data[0]) ? $data[0] : [];
      $conditions = [];
      if ($data) {
        $conditions[] = ['sb.back_order_id', $id, '='];
        // 获取损耗详情
        $detail = StoreBackService::getInstance()->getBackJoinStore($conditions);
        $data['detail'] = $detail;
      }
      return $this->send200Response($data);

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 导出退货单
     */
    public function export(Request $request) {
      $backEnd = $request->param('backEnd', '');
      $backStart = $request->param('backStart', '');
      $backStart = $backStart ? date('Y-m-d', strtotime($backStart)) : '';
      $backEnd = $backEnd ? date('Y-m-d', strtotime($backEnd) + (24 * 60 * 60)) : '';

      $conditions = [];
      if ($backStart) {
        $conditions[] = ['so.back_time', $backStart, '>='];
      }
      if ($backEnd) {
        $conditions[] = ['so.back_time', $backEnd, '<'];
      }
      $order = ['so.back_time' => 'asc', 'sb.id' => 'desc'];
      $list = StoreBackService::getInstance()->getBackJoinStore($conditions, $order);

      $spreadsheet = new Spreadsheet();
      $sheet = $spreadsheet->getActiveSheet();
      $sheet->getColumnDimension('A')->setWidth(15);
      $sheet->getColumnDimension('B')->setWidth(20);
      $sheet->getColumnDimension('C')->setWidth(15);
      $sheet->getColumnDimension('D')->setWidth(15);
      $sheet->getColumnDimension('E')->setWidth(15);
      $sheet->getColumnDimension('F')->setWidth(15);
      $sheet->getColumnDimension('G')->setWidth(15);

      $sheet->setCellValue('A1', '退货日期');
      $sheet->setCellValue('B1', '退货单号');
      $sheet->setCellValue('C1', '分类');
      $sheet->setCellValue('D1', '商品名称');
      $sheet->setCellValue('E1', '单位');
      $sheet->setCellValue('F1', '数量');
      $sheet->setCellValue('G1', '门店');

      $row = 1;
      foreach($list as $v){
        $row++;
        $sheet->setCellValue("A{$row}", date('Y-m-d', strtotime($v['back_time'])));
        $sheet->setCellValue("B{$row}", $v['apply_number']);
        $sheet->setCellValue("C{$row}", $v['category_name']);
        $sheet->setCellValue("D{$row}", $v['name']);
        $sheet->setCellValue("E{$row}", $v['unit']);
        $sheet->setCellValue("F{$row}", $v['count']);
        $sheet->setCellValue("G{$row}", $v['basename']);
      }
      header("Content-type: application/octet-stream;charset=utf-8");
      header('Content-Disposition: attachment;filename="冠记-退货导出.xlsx"');
      header('Cache-Control: max-age=0');
      $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
      $writer->save('php://output');
    }
}
